package com.gsx.service.Impl;

import com.github.pagehelper.PageHelper;
import com.gsx.entity.*;
import com.gsx.exception.NotFoundException;
import com.gsx.mapper.BlogMapper;
import com.gsx.service.BlogService;
import com.gsx.utils.MarkdownUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Override
    public Blog getBlog(Long id) {
        return blogMapper.getBlog(id);
    }

    @Override
    public List<Blog_Type> getListBlog(int page, int size,Blog_Type blog_type) {
        PageHelper.startPage(page,size);
        return blogMapper.getListBlog(blog_type);
    }

    public List<Blog>  getAllBlog(int page,int size){
        PageHelper.startPage(page, size);
        return blogMapper.getAllBlog();
    }

    public List<Blog> getNewBlog(){
        return blogMapper.getNewBlog();
    }

    public List<Blog_Type_User> getBlog_Type_User(int page,int size){
        PageHelper.startPage(page,size);
        return blogMapper.getBlog_Type_User();
    }

    public List<Blog_Type_User> getByBlogTitle(int page,int size,String title){
        PageHelper.startPage(page,size);
        return blogMapper.getByBlogTitle(title);
    }

    public Blog_Type_User getB_T_U(Long id){
        return blogMapper.getB_T_U(id);
    }

    public Blog_Type_User getB_T_U_Front(Long id){
        Blog_Type_User blog_type_user=blogMapper.getB_T_U_Front(id);
        if(blog_type_user==null){
            throw new NotFoundException("该博客不存在");
        }
        String content=blog_type_user.getContent();
        /*处理markdown成为html*/
        blog_type_user.setContent(MarkdownUtil.markdownToHtmlExtensions(content));
        return blog_type_user;
    }

    @Transactional
    @Override
    public int saveBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
        return blogMapper.saveBlog(blog);
    }

    @Transactional
    @Override
    public int updateBlog(Blog blog) {
        if(blogMapper.getBlog(blog.getBlog_id())==null){
            throw new NotFoundException("该博客不存在");
        }
        blog.setUpdateTime(new Date());
        return blogMapper.updateBlog(blog);
    }

    @Transactional
    @Override
    public int deleteBlog(Long id) {
        return blogMapper.deleteBlog(id);
    }

    public Long[] getId(){
        return blogMapper.getId();
    }

    /*查询所有文章的年份*/
    public String[] getYears(){
        return blogMapper.getYears();
    }
    /*根据年份来查询数据*/
    public List<Blog> getByYear(String year){
        return blogMapper.getByYear(year);
    }


    public List<Blog_Type_User> getSelfBlog(Long user_id,int page,int size){
        PageHelper.startPage(page,size);
        return blogMapper.getSelfBlog(user_id);
    }
}
